package com.symbio.ishenapi.laboratory.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.bean.QueryModel;
import com.symbio.ishenapi.laboratory.bean.InfoattrRequest;
import com.symbio.ishenapi.laboratory.bean.LaboratoryIndicators;
import com.symbio.ishenapi.laboratory.bean.LaboratoryIndicatorsResponse;
import com.symbio.ishenapi.laboratory.bean.LaboratoryResponse;
import io.jboot.db.annotation.Table;
import com.symbio.ishenapi.laboratory.model.base.BaseTmLaboratoryInfo;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Generated by Jboot.
 */
@Table(tableName = "tm_laboratory_info", primaryKey = "id")
public class TmLaboratoryInfo extends BaseTmLaboratoryInfo<TmLaboratoryInfo> {
    public  static TmLaboratoryInfo dao=new TmLaboratoryInfo();


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据化验订单id查询关联配置表id
     * @date 下午 4:22   2018/10/25
     * @return java.util.List<java.lang.String>
     */
    public List<String> getid(Object idd){
        String sql="select id ids from tm_laboratory_info_attr where laboratoryInfoId="+idd+"";
        List<TmLaboratoryInfo> lisst=dao.find(sql);
        List<String> li=new ArrayList<String>();
        for(int i =0;i<lisst.size();i++){
            String id=lisst.get(i).getStr("ids");
            li.add(id);
        }
        return li;
    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userid、指标列表查询所有化验单数据
     * @date 上午 10:04   2018/10/26
     * @return java.util.List<com.symbio.ishenapi.laboratory.bean.LaboratoryIndicatorsResponse>
     */
    public List<LaboratoryIndicatorsResponse> seleByInfoattrId(InfoattrRequest inattr){
        String sql="SELECT l.userId userid,l.checkTime checktime,ls.indicatorsId indicatorsid,ins.indicatorsName indicatorsname, ls.indicatorsResult indicatorsresult FROM tm_laboratory_info_attr ls JOIN tm_laboratory_info l ON l.id = ls.laboratoryInfoId JOIN tm_laboratory_indicators ins on ls.indicatorsId=ins.id  WHERE ls.indicatorsId in ("+inattr.getIndicatorsId()+") and  l.laboratoryType = 0  and  l.userId="+inattr.getUserId()+" ";
        List<Record> list=Db.find(sql);
        List<LaboratoryIndicatorsResponse> listre=new ArrayList<LaboratoryIndicatorsResponse>();
        for(int i=0;i<list.size();i++){
            LaboratoryIndicatorsResponse labo=new LaboratoryIndicatorsResponse();
            labo.setUserId(list.get(i).getStr("userid"));
            labo.setCheckTime(list.get(i).getStr("checktime"));
            labo.setIndicatorsId(list.get(i).getStr("indicatorsid"));
            labo.setIndicatorsResult(list.get(i).getStr("indicatorsresult"));
            labo.setIndicatorsname(list.get(i).getStr("indicatorsname"));
            listre.add(labo);
        }
        return listre;
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userid、指标id查询日期分组
     * @date 上午 10:21   2018/10/26
     * @return java.util.List<java.lang.String>
     */
    public List<String> getChecktime(InfoattrRequest inr){
        String sql="SELECT l.checkTime checktime  FROM tm_laboratory_info_attr ls JOIN tm_laboratory_info l ON l.id = ls.laboratoryInfoId JOIN tm_laboratory_indicators ins on ls.indicatorsId=ins.id  WHERE ls.indicatorsId in ("+inr.getIndicatorsId()+") and l.userId="+inr.getUserId()+" and l.laboratoryType = 0  GROUP BY l.checkTime";
        List<TmLaboratoryInfo> list=dao.find(sql);
        List<String> lsitstr=new ArrayList<String>();
        for(int i=0;i<list.size();i++){
            String time=list.get(i).getStr("checktime");
            lsitstr.add(time);
        }
        return lsitstr;
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userid、化验单类型（0数据，1图片）查询所有化验单数据
     * @date 上午 11:55   2018/10/30
     * @return java.util.List<com.symbio.ishenapi.laboratory.bean.LaboratoryResponse>
     */
    public List<LaboratoryResponse> seleLaboraByTime(TmLaboratoryInfo tmb){
        String sql="";
        if("1".equals(tmb.getLaboratoryType())){
            sql="SELECT\n" +
                    "\tinfo.id id,\n" +
                    "\tinfo.checkTime time,\n" +
                    "\tinfo.laboratoryPath path,info.createTime\n" +
                    "FROM\n" +
                    "\ttm_laboratory_info info\n" +
                    "WHERE\n" +
                    "\tinfo.userId = "+tmb.getUserId()+"\n" +
                    "AND info.laboratoryType = "+tmb.getLaboratoryType()+"\n" +
                    "AND info.status = 1";

        }else{
            sql="SELECT\n" +
                    "\tinfo.id id,\n" +
                    "\tinfo.checkTime time,\n" +
                    "\tbasi.laboratoryName labname,\n" +
                    "\tinfo.laboratoryPath path,info.createTime\n" +
                    "FROM\n" +
                    "\ttm_laboratory_info info\n" +
                    "JOIN tm_laboratory_basis basi ON info.laboratoryId = basi.id\n" +
                    "WHERE\n" +
                    "\tinfo.userId = "+tmb.getUserId()+"\n" +
                    "AND info.laboratoryType = "+tmb.getLaboratoryType()+"\n" +
                    "AND info.status = 1";
        }

        List<TmLaboratoryInfo> list=dao.find(sql);
        List<LaboratoryResponse> lare=new ArrayList<LaboratoryResponse>();
        for(int i = 0;i<list.size();i++){
            LaboratoryResponse  laresp=new LaboratoryResponse();
            String id=list.get(i).getStr("id");
            laresp.setId(id);
            String time=list.get(i).getStr("time");
            laresp.setCheckTime(time);
            String name=list.get(i).getStr("labname");
            laresp.setLaboratoryNmae(name);
            String path=list.get(i).getStr("path");
            laresp.setPath(path);
            String createTime=list.get(i).getStr("createTime");
            laresp.setCreateTime(createTime);
            lare.add(laresp);
        }
        return lare;

    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通过userid、化验单类型查询所有数据日期分组
     * @date 下午 2:32   2018/10/30
     * @return java.util.List<java.lang.String>
     */
    public List<String> getGroupByTime(TmLaboratoryInfo tmb){
        String sql="";
        if("1".equals(tmb.getLaboratoryType())){
            sql="SELECT\n" +
                    "\tinfo.checkTime\n" +
                    "FROM\n" +
                    "\ttm_laboratory_info info\n" +
                    "WHERE\n" +
                    "\tinfo.userId = "+tmb.getUserId()+"\n" +
                    "AND info.laboratoryType = "+tmb.getLaboratoryType()+"\n" +
                    "AND info.status = 1\n" +
                    "GROUP BY info.checkTime ORDER BY info.checkTime desc";
        }else{
            sql="SELECT\n" +
                    "\tinfo.checkTime\n" +
                    "FROM\n" +
                    "\ttm_laboratory_info info\n" +
                    "JOIN tm_laboratory_basis basi ON info.laboratoryId = basi.id\n" +
                    "WHERE\n" +
                    "\tinfo.userId = "+tmb.getUserId()+"\n" +
                    "AND info.laboratoryType = "+tmb.getLaboratoryType()+"\n" +
                    "AND info.status = 1\n" +
                    "GROUP BY info.checkTime ORDER BY info.checkTime desc";
        }

        List<TmLaboratoryInfo> list=dao.find(sql);
        List strlist=new ArrayList();
        for(int i=0;i<list.size();i++){
            Date date=list.get(i).getCheckTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String strdate=sdf.format(date);
            strlist.add(strdate);
        }
        return strlist;

    }

    /**
     * wy:分页查询数据化验单
     */
    public Map findMethLaboratory(QueryModel queryModel,String userId){
        Map<String,Object> map = new HashMap<>();
        String countSql = "SELECT count(t.id) from tm_laboratory_info t where  t.userId = "+userId ;
        String sql = "SELECT * from tm_laboratory_info t  where t.userId = "+userId+"  ORDER BY t.createTime DESC limit "+queryModel.getPage()+","+queryModel.getLimit()+"";
        int count = Db.queryInt(countSql);
        List<Record> list = Db.find(sql);
        map.put("count",count);
        map.put("data",list);
        return map;
    }

    /**
     * wy:批量禁用数据化验单
     */
    public boolean delLaboratory(String ids){
        boolean flag = false;
        if (ids != null && !"".equals(ids)){
            String sql = "UPDATE tm_laboratory_info t SET t.status ='0' WHERE t.id IN ("+ids+")";
            int i = Db.update(sql);
            if (i > 0){
                flag = true;
            }
        }
        return flag;
    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通过userid、laboratoryid、checktime查询是否已经存在
     * @date 下午 5:28   2018/11/11
     * @return int
     */
    public int getCountLaboratory(LaboratoryIndicators lai){
        String sql="select count(1) num from tm_laboratory_info where  userid="+lai.getUserId()+" AND laboratoryId="+lai.getLoboratoryId()+" and checkTime='"+lai.getCheckTime()+"'";
        TmLaboratoryInfo tmim=dao.findFirst(sql);
        String co=tmim.getStr("num");
        return Integer.valueOf(co);

    }

    /**
     * @Author Jianghy
     * @Description 通过userid、laboratoryid、统计开始time和结束time 分组查询指标信息
     * @date  2018/12/12
     * @return
     */
    public List<Record> getIndicatorsTrendChart(String startTime, String endTime, String laboratoryid, String userId){
        String sql = "select a.indicatorsResult,b.createTime from tm_laboratory_info_attr a left join  tm_laboratory_info b  " +
                "on b.id = a.laboratoryInfoId\n" +
                "where b.userId = "+userId+" and b.status = 1 and b.laboratoryType = 0 and a.indicatorsId = "+laboratoryid+" and a.status = 1 " +
                "and a.laboratoryInfoId in \n" +
                "(select id from tm_laboratory_info  where userId = 1 and status = 1 and laboratoryType = 0) " +
                "and b.createTime  between "+startTime+" and "+endTime+" order by b.createTime asc";
        System.out.println(sql);
        return Db.find(sql);
    }
}
